# Load Data
setwd("/users/josephphillips/Dropbox/COVID-19/Data/FC Data Repository")
df <- read.csv("Canada 2.csv",header=T,sep=",")

# Load GLMNet Function
run_LASSO <- function(data, yvar){
  
  set.seed(2334)
  
  var_quo <- enquo(yvar)
  
  ## select x variables 
  dat <- data %>%
    dplyr::select(university, agegroup, male, married, frequentchurch, region, left, right, ideology1, knowledge, nonwhite, polinterest, trust_healthgov, total_media_trust, hotspot)
  
  ## select y-variable
  out1 <- data %>% 
    dplyr::select(!!var_quo) %>% pull() # save as a vector instead of a data-frame to use model.matrix()
  
  merged1 <- cbind(out1, dat) %>% na.omit() ## merge and delete NA
  x1 <- model.matrix(out1 ~ ., data = merged1) ## make model matrix
  y1 <- merged1$out1 ## select y-variable from NA-deleted dataset
  mod1 <- cv.glmnet(x1, y1, alpha = 1, family = "gaussian") ## run lasso model
  coef <- coef(mod1) ## get coef  
  
  list(merged1, x1, y1, mod1, coef)
  
}

# Lasso Regressions
lasso.h1 <- run_LASSO(df,targeted_false) ## age 45-54, age 55, age 65+, male, frequent church, left, right, ideology, knowledge, nonwhite, political interest, health trust
lasso.rq1.false <- run_LASSO(df,nontargeted_false) # age 45-54, age 55-64, age 65+, male, frequent church, ideology, knowledge, nonwhite, health trust, media trust
lasso.rq1.false.noleak <- run_LASSO(df,nontargeted_false_noleak) # age 45-54, age 55-64, age 65+, male, frequent church, left, ideology, knowledge, nonwhite, polinterest
lasso.rq1.true <- run_LASSO(df,nontargeted_true) # age 45-54, age 55-64, age 65, male, frequent church, Quebec, ideology, knowledge, nonwhite, polinterest,health trust, media trust, hotspot
lasso.canada2.con_theory1 <- run_LASSO(df,accuracy_con_theory1) # frequentchurch, ideology1, knowledge, trust_healthgov
lasso.canada2.con_theory5 <- run_LASSO(df,accuracy_con_theory5) # age 45, age 55, frequentchurch, right, ideology1, knowledge, nonwhite, trust_healthgov
lasso.canada2.False1 <- run_LASSO(df,accuracy_False1) # age 65+, frequentchurch, ideology1, knowledge, nonwhite
lasso.canada2.False4 <- run_LASSO(df,accuracy_False4) # age 65+, frequentchurch, quebec, ideology1, knowledge, nonwhite, trust_healthgov, hotspot
lasso.canada2.False2 <- run_LASSO(df,accuracy_False2) # age 65, frequent church, quebec, ideology1, nonwhite, total_media_trust
lasso.canada2.False3 <- run_LASSO(df,accuracy_False3) # age 45, age 65, male, frequentchurch, quebec, left, ideology1, knowledge, nonwhite
lasso.canada2.edge1 <- run_LASSO(df,accuracy_edge1) # age 45, age 55, age 65+, frequentchurch, left, ideology1, knowledge, nonwhite, trust_healthgov
lasso.canada2.con_theory2 <- run_LASSO(df,accuracy_con_theory2) # ideology1, trust_healthgov
lasso.canada2.f_cure_1 <- run_LASSO(df,accuracy_f_cure_1) # age 65+, frequentchurch, ideology1, knowledge, nonwhite
lasso.canada2.f_cure_2 <- run_LASSO(df,accuracy_f_cure_2) # age 45, age 65+, frequentchurch, ontario, ideology1, knowledge, nonwhite, trust_healthgov
lasso.canada2.f_cure_3 <- run_LASSO(df,accuracy_f_cure_3) # age 45, age 55, age 65, male, frequent church, left, ideology1, knowledge, nonwhite, health trust
lasso.canada2.True1 <- run_LASSO(df,accuracy_True1) # age 45, age 55, male, frequentchurch, ideology1, knowledge, nonwhite, trust_healthgov
lasso.canada2.True2 <- run_LASSO(df,accuracy_True2) # age 45, age 55, age 65+, male, frequentchurch, ideology1, knowledge, nonwhite, trust_healthgov
lasso.canada2.True3 <- run_LASSO(df,accuracy_True3) # age 55, age 65, male, frequentchurch, ideology1, knowledge, trust_healthgov
lasso.canada2.True4 <- run_LASSO(df,accuracy_True4) # male, frequentchurch, quebec, ideology1, knowledge, trust_healthgov
lasso.canada2.True5 <- run_LASSO(df,accuracy_True5) # male, quebec, ideology1, knowledge, nonwhite, trust_healthgov
lasso.canada2.True6 <- run_LASSO(df,accuracy_True6) # male, quebec, ideology1, political interest, trust_healthgov, total media trust
lasso.canada2.change3 <- run_LASSO(df,accuracy_change3) # age 55, age 65, ideology1, health trust
lasso.canada2.change1 <- run_LASSO(df,accuracy_change1) # N/A
lasso.canada2.True7 <- run_LASSO(df,accuracy_True7) # university, left, polinterest, trust_healthgov, total_media_trust
